package com.onenine.chat.message;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.TextView;

import com.onenine.base.utils.AppConfig;
import com.onenine.chat.commons.ImageLoader;
import com.onenine.chat.commons.ViewHolder;
import com.onenine.chat.commons.IMessage;

import java.util.List;


public abstract class BaseMessageViewHolder<MESSAGE extends IMessage>
        extends ViewHolder<MESSAGE> {

    protected Context mContext;
    protected float mDensity;
    protected int mPosition;
    protected boolean mIsSelected;
    protected ImageLoader mImageLoader;

    protected MsgListAdapter.OnMsgLongClickListener<MESSAGE> mMsgLongClickListener;
    protected MsgListAdapter.OnMsgClickListener<MESSAGE> mMsgClickListener;
    protected MsgListAdapter.OnAvatarClickListener<MESSAGE> mAvatarClickListener;
    protected MsgListAdapter.OnMsgStatusViewClickListener<MESSAGE> mMsgStatusViewClickListener;
    protected MediaPlayer mMediaPlayer;
    protected boolean mScroll;
    protected List<MsgListAdapter.Wrapper> mData;

    public BaseMessageViewHolder(View itemView) {
        super(itemView);
    }

    /**
     * 设置时间间可见性
     *
     * @param tvDate
     * @param position
     */
    protected void setDatetimeVisibility(TextView tvDate, int position) {
        boolean showTime = isDateVisibility(position);
        if (showTime) {
            tvDate.setVisibility(View.VISIBLE);
        } else {
            tvDate.setVisibility(View.GONE);
        }
    }

    /**
     * 聊天时间是否显示
     * 2条消息以3分钟为间隔
     *
     * @param position
     * @return
     */
    private boolean isDateVisibility(int position) {
        MsgListAdapter.Wrapper wrapper = mData.get(position);
        IMessage curMessage = (IMessage) wrapper.getItem();
        if (position == mData.size() - 1) {
            return true;
        } else {
            IMessage prevMessage = (IMessage) mData.get(position + 1).getItem();
            return curMessage.getDatetime() - prevMessage.getDatetime() > AppConfig.THREE_MINUTE;
        }
    }

}